package com.elite.www;

import cn.kmpro.util.KmproSign;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

public class callReSearch extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(callReSearch.class);

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String redirString = "";

        log.info("StartServlet接收调用报文信息：" + System.getProperty("line.separator") + request.getQueryString());

        String jsonq = null;
        String jsonst = null;
        String jsonURL = null;
        String jsonKey = null;
        String jsonLoginname = null;

        jsonq = request.getParameter("q");
        jsonst = request.getParameter("st");
        jsonKey = request.getParameter("key");
        jsonLoginname = request.getParameter("loginname");
        jsonURL = request.getParameter("url");

        log.debug("StartServlet 解析调用报文(url):" + System.getProperty("line.separator") + jsonURL);
        log.debug("StartServlet 解析调用报文(key):" + System.getProperty("line.separator") + jsonKey);
        log.debug("StartServlet 解析调用报文(loginname):" + System.getProperty("line.separator") + jsonLoginname);

        String sign = KmproSign.sign(jsonLoginname, jsonKey);

        log.debug("StartServlet 生成签名(sign):" + sign);

        redirString = jsonURL + "?" + "q=" + URLEncoder.encode(jsonq, "UTF-8") + "&st=" + jsonst + "&loginName=" + jsonLoginname + "&sign=" + sign + "";

        log.debug("StartServlet 返回重定向URL信息:" + System.getProperty("line.separator") + redirString);

        response.sendRedirect(redirString);
    }
}